package cn.chiship.framework.upms.biz.system.service.impl;

import cn.chiship.framework.upms.biz.system.mapper.UpmsQuartzJobMapper;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import cn.chiship.framework.upms.biz.system.entity.UpmsQuartzJob;
import cn.chiship.framework.upms.biz.system.entity.UpmsQuartzJobExample;
import cn.chiship.framework.upms.biz.system.service.UpmsQuartzJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 定时任务业务接口实现层
 * 2021/12/2
 *
 * @author lijian
 */
@Service
public class UpmsQuartzJobServiceImpl extends BaseServiceImpl<UpmsQuartzJob, UpmsQuartzJobExample> implements UpmsQuartzJobService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsQuartzJobServiceImpl.class);

    @Resource
    UpmsQuartzJobMapper upmsQuartzJobMapper;


    @Override
    public BaseResult insertSelective(UpmsQuartzJob upmsQuartzJob) {
        UpmsQuartzJobExample quartzJobExample = new UpmsQuartzJobExample();
        quartzJobExample.createCriteria().andJobClassEqualTo(upmsQuartzJob.getJobClass()).andJobMethodEqualTo(upmsQuartzJob.getJobMethod());
        List<UpmsQuartzJob> upmsQuartzJobs = upmsQuartzJobMapper.selectByExample(quartzJobExample);
        if (upmsQuartzJobs.isEmpty()) {
            return super.insertSelective(upmsQuartzJob);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "类【" + upmsQuartzJob.getJobClass() + "】下的方法【" + upmsQuartzJob.getJobMethod() + "】任务已存在，请重新输入");
        }
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(UpmsQuartzJob upmsQuartzJob) {
        UpmsQuartzJobExample quartzJobExample = new UpmsQuartzJobExample();
        quartzJobExample.createCriteria().andJobClassEqualTo(upmsQuartzJob.getJobClass()).andJobMethodEqualTo(upmsQuartzJob.getJobMethod()).andIdNotEqualTo(upmsQuartzJob.getId());
        List<UpmsQuartzJob> upmsQuartzJobs = upmsQuartzJobMapper.selectByExample(quartzJobExample);
        if (upmsQuartzJobs.isEmpty()) {
            return super.updateByPrimaryKeySelective(upmsQuartzJob);
        } else {
            return BaseResult.error(BaseResultEnum.EXCEPTION_DATA_BASE_REPEAT, "类【" + upmsQuartzJob.getJobClass() + "】下的方法【" + upmsQuartzJob.getJobMethod() + "】任务已存在，请重新输入");
        }
    }
}
